package org.itsolutions.javaBackup.gui.web.admin.view;

import org.itsolutions.javaBackup.gui.web.common.AStatefullView;
import org.itsolutions.javaBackup.gui.web.common.ApplicationDataHolder;
import org.itsolutions.javaBackup.gui.web.common.components.IView;
import org.itsolutions.javaBackup.gui.web.common.components.JBAccordion;
import org.itsolutions.javaBackup.gui.web.common.components.JBButton;
import org.itsolutions.javaBackup.gui.web.common.components.JBVerticalLayout;

import com.google.inject.Inject;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComponentContainer;

/**
 * 
 * @author Tomas Fecko
 */
public final class AdminMenu extends AStatefullView<AdminMenu> {
	
	@Inject JBAccordion				mMainContainer;
	@Inject ApplicationDataHolder	mAppHolder;
	@Inject AdminMainView			mAdminMainView;
	@Inject BackupView 				mBackupView;
	
	@Inject JBVerticalLayout 		mDataTab;
	@Inject JBButton 				mBackupMenuItem;
	@Inject JBButton 				mLogout;
	
	@Inject JBVerticalLayout 		mGeneralConfig;
	
	@Override
	public ComponentContainer getMainContainer() {
		return mMainContainer;
	}

	@Override
	protected AdminMenu initOnce() {
		// GENERAL CONFIG TAB
		mGeneralConfig.setSpacing(true);
		mGeneralConfig.setMargin(true);

		mLogout.setCaption("LOGOUT.MNI");
		mLogout.setStyleName("link");
		mLogout.addListener(new LogoutButtonClickListener(mAppHolder));
		mGeneralConfig.addComponent(mLogout);

		mMainContainer.addTab(mGeneralConfig, mAppHolder.getString("GENERAL_CONFIG.TBN"), null);

		// DATA TAB
		mDataTab.setSpacing(true);
		mDataTab.setMargin(true);


		mBackupMenuItem.setCaption("BACKUP_SERVICE.MNI");
		mBackupMenuItem.setStyleName("link");
		mBackupMenuItem.addListener(new MenuItemClickListener(mAdminMainView, mBackupView));
		mDataTab.addComponent(mBackupMenuItem);
		mMainContainer.addTab(mDataTab, mAppHolder.getString("BACKUP.TBN"), null);
		return this;
	}
	
	
	/**
	 * 
	 */
	private static final class MenuItemClickListener implements Button.ClickListener {
		
		private static final long 	serialVersionUID = 4618543161803153066L;
		private final AdminMainView	mAdminMainView;
		private final IView<?>		mView;
		
		/**
		 * @param pMenu
		 */
		private MenuItemClickListener(AdminMainView pAdminMainView, IView<?> pView) {
			mAdminMainView = pAdminMainView;
			mView = pView;
		}

		@Override
		public void buttonClick(com.vaadin.ui.Button.ClickEvent pEvent) {
			mView.init();
			mAdminMainView.setContent(mView.getMainContainer());
		}
	}

	/**
	 * 
	 */
	private static final class LogoutButtonClickListener implements Button.ClickListener {
		
		private static final long 			serialVersionUID = 4618543161803153066L;
		private final ApplicationDataHolder mAppDataHolder;
		
		/**
		 * @param pAppDataHolder
		 */
		private LogoutButtonClickListener(ApplicationDataHolder pAppDataHolder) {
			mAppDataHolder = pAppDataHolder;
		}

		@Override
		public void buttonClick(com.vaadin.ui.Button.ClickEvent pEvent) {
			mAppDataHolder.logout();
		}
	}
}





